import { IORecords, QRToken } from '../../../services/baseClass'
import QR from '../../../utils/weapp-qrcode/weapp-qrcode'
import services from "../../../services/index"
import { formatDateStr } from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showQrcode: false,
    ioRecord : IORecords,
    expireTime:null
  },
  onTest() {
    console.log(this.data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function() {
    // TODO orderinfo by orderID
    //const reportID = wx.getStorageSync("myReport/myReport")
    this.getRecordDetail();
    this.startInter();
  },
  getRecordDetail : async function(){
   // const reportID = wx.getStorageSync("myReport/myReport")
    const reportID = 4;
    let res = await services.API.iORecords.getIORecordByID.request({ID:reportID});
    
    if(res&&res.data){
      let record = res.data;
      if (record.status == 'SCHOOL_PASS') {
        console.log(record);
        this.setData({ showQrcode: true })
        const qrToken = record.qrToken;
        this.drawQrcode(qrToken);
        wx.setStorageSync("QRCODE_EXPIRE",qrToken?.expireTime);
        
      }
      this.setData({ioRecord:record?record:null});
    }
  },
  startInter : function(){
    let that = this;
    
    setInterval(
        function () {
            // TODO 你需要无限循环执行的任务
            const expire = wx.getStorageSync("QRCODE_EXPIRE");
            let now = Date.parse(new Date().toString());
            console.log(now+"****"+expire);
            if(now>expire){
              that.getRecordDetail();
            }            
        }, 1000);    
  },

  drawQrcode:function(qrCode){
    if(qrCode){
      console.log("cccc",qrCode);
      this.setData({expireTime: formatDateStr(qrCode.expireTime)});
      let qrcodeStr = JSON.stringify(qrCode);
      const qrSrc = QR.drawImg(qrcodeStr, {
        typeNumber: 4,
        errorCorrectLevel: 'M',
        size: 500
      })
      this.setData({qrSrc,});      
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})